<?php 
	session_start(); 
	
	/* Messages to inform user */
	$MSG_error = "";
	
	/* Used for communication with the database */
	require_once("classes/Mail.class.php");
	include("classes/database.class.php");
	include("includes/define.php");
	$bd = new Database(MYSQL_DB_HOST,MYSQL_DB_LOGIN,MYSQL_DB_PASSWORD,MYSQL_DB_NAME);
	
	date_default_timezone_set('Europe/Paris');
	$year = date("Y");
	
	/* Try to save the participant's profile - If there is an empty field (or more) */
	if(isset($_POST['infos']) && ($_POST['email']=="" || $_POST['id']=="" || !isset($_POST['gender']) || ($_POST['monthlist']==1 && $_POST['yearlist']==$year)))
	{
		$MSG_error = "<p class='error'>Tous les champs sont nécessaires.</p>";
	}
	/* Try to save the participant's profile - New participant */
	elseif(isset($_POST['infos']) && !isset($_SESSION['participantId']))
	{ 
		$DATABASE_isMail = $bd->query("SELECT participantId FROM participants WHERE mail = '".$_POST['email']."';");
		$DATABASE_isID = $bd->query("SELECT participantId FROM participants WHERE participantId = '".$_POST['id']."';");
		$bd->close();
		
		if(mysql_num_rows($DATABASE_isMail) == 0 && mysql_num_rows($DATABASE_isID)==0)
		{
			$_SESSION['participantId'] = $_POST['id'];
			$_SESSION['gender'] = $_POST['gender'];
			
			$bd->query("INSERT INTO participants (participantId, gender, birthMonth, birthYear, mail) VALUES ('".$_SESSION['participantId']."','".$_POST['gender']."','".$_POST['monthlist']."','".$_POST['yearlist']."','".$_POST['email']."');");
			$bd->close();
			
			header("Location: http://".$_SERVER['SERVER_NAME']."/participant.php");
		}
		else
		{
			$MSG_error = "<p class='error'>Adresse mail et/ou identifiants non disponibles.</p>";
		}
	}
	/* Try to save the participant's profile - Already saved participant */
	elseif(isset($_POST['infos']) && isset($_SESSION['participantId']))
	{ 
		$DATABASE_participant = $bd->query("SELECT * FROM participants WHERE participantId = '".$_SESSION['participantId']."';");
		$DATABASE_participant = mysql_fetch_assoc($DATABASE_participant);
		if($_SESSION['participantId'] != $_POST['id'] && $DATABASE_participant['mail'] != $_POST['email'] )
		{
			$DATABASE_oldID = $bd->query("SELECT participantId FROM participants WHERE participantId = '".$_POST['id']."';");
			$DATABASE_oldMail = $bd->query("SELECT participantId FROM participants WHERE mail = '".$_POST['email']."';");
			$bd->close();
			
			if(mysql_num_rows($DATABASE_oldMail) == 0 && mysql_num_rows($DATABASE_oldID)==0)
			{
				$bd->query("UPDATE participants SET participantId = '".$_POST['id']."', gender = '".$_POST['gender']."', birthMonth='".$_POST['monthlist']."', birthYear = '".$_POST['yearlist']."', mail = '".$_POST['email']."' WHERE participantId = '".$_SESSION['participantId']."';");
				$bd->close();
				
				$_SESSION['participantId'] = $_POST['id'];
				
				header("Location: http://".$_SERVER['SERVER_NAME']."/participant.php");
			}
			else
			{
				$MSG_error = "<p class='error'>Adresse mail et identifiant non disponibles.</p>";
			}
		}
		elseif($_SESSION['participantId'] != $_POST['id'])
		{
			$DATABASE_isID = $bd->query("SELECT participantId FROM participants WHERE participantId = '".$_POST['id']."';");
			$bd->close();
			
			if(mysql_num_rows($DATABASE_isID)==0)
			{
				$bd->query("UPDATE participants SET participantId = '".$_POST['id']."', gender = '".$_POST['gender']."', birthMonth='".$_POST['monthlist']."', birthYear = '".$_POST['yearlist']."', mail = '".$_POST['email']."' WHERE participantId = '".$_SESSION['participantId']."';");
				$bd->close();
				
				$_SESSION['participantId'] = $_POST['id'];
				
				header("Location: http://".$_SERVER['SERVER_NAME']."/participant.php");
			}
			else
			{
				$MSG_error = "<p class='error'>Identifiant non disponible.</p>";
			}
		}
		elseif($DATABASE_participant['mail'] != $_POST['email'])
		{
			$DATABASE_isMail = $bd->query("SELECT participantId FROM participants WHERE mail = '".$_POST['email']."';");
			$bd->close();
			
			if(mysql_num_rows($DATABASE_isMail)==0)
			{
				$bd->query("UPDATE participants SET participantId = '".$_POST['id']."', gender = '".$_POST['gender']."', birthMonth='".$_POST['monthlist']."', birthYear = '".$_POST['yearlist']."', mail = '".$_POST['email']."' WHERE participantId = '".$_SESSION['participantId']."';");
				$bd->close();
				
				$_SESSION['participantId'] = $_POST['id'];
				
				header("Location: http://".$_SERVER['SERVER_NAME']."/participant.php");
			}
			else
			{
				$MSG_error = "<p class='error'>Adresse mail non disponible.</p>";
			}
		}
		else
		{
			$bd->query("UPDATE participants SET participantId = '".$_POST['id']."', gender = '".$_POST['gender']."', birthMonth='".$_POST['monthlist']."', birthYear = '".$_POST['yearlist']."', mail = '".$_POST['email']."' WHERE participantId = '".$_SESSION['participantId']."';");
			$bd->close();
			
			$_SESSION['participantId'] = $_POST['id'];
			
			header("Location: http://".$_SERVER['SERVER_NAME']."/participant.php");
		}		
	}
?>
	
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
<title>Informations étudiant</title>
</head>

<body>
	<div id="participant">
		<h1>Quelques informations</h1>
		
		<?php
			if(!isset($_SESSION['participantId']))
			{
				echo "<form method='post' action='informations.php'>
						<label for='id'>2 premières lettres du prénom et du nom suivi des 4 derniers chiffres de votre numéro de téléphone</label><br/>
						<input type='text' name='id' id='id'></textarea><br/><br/>
						
						<input type='email' name='email' id='email' placeholder='adresse e-mail'></textarea> <br/><br/>
						
						<input type='radio' name='gender' value=1 id='masculin'/><label for='masculin'>Masculin</label>
						<input type='radio' name='gender' value=2 id='feminin'/><label for='feminin'>Féminin</label> <br/><br/>
						
						<label name='birthyear'>Mois et année de naissance :</label> 
						<select name='monthlist' onChange='combo(this, 'birthyear')'>
							<option value=1>janvier</option> 
							<option value=2>février</option>
							<option value=3>mars</option>
							<option value=4>avril</option>
							<option value=5>mai</option>
							<option value=6>juin</option>
							<option value=7>juillet</option>
							<option value=8>août</option>
							<option value=9>septembre</option>
							<option value=10>octobre</option>
							<option value=11>novembre</option>
							<option value=12>décembre</option>
						</select> 
						<select name='yearlist' onChange='combo(this, 'birthyear')'>";
						
						for($i = $year; $i > $year - 100; $i--)
						{
							echo "<option>".$i."</option>";
						}
						
						echo "</select> <br/><br/>
								
						<input type='submit' name='infos' value='Enregistrer' />
					</form>";

				echo $MSG_error;
			}
			else
			{
				$DATABASE_infosparticipant = $bd->query("SELECT * FROM participants WHERE participantId = '".$_SESSION['participantId']."';");
				$bd->close();
				
				$DATABASE_infosparticipant = mysql_fetch_assoc($DATABASE_infosparticipant);
				
				echo "<form method='post' action='informations.php'>
						<label for='id'>2 premières lettres du prénom et du nom suivi des 4 derniers chiffres de votre numéro de téléphone</label><br/>
						<input type='text' name='id' id='id' value='".$DATABASE_infosparticipant['participantId']."'></textarea><br/><br/>
						
						<input type='email' name='email' id='email' placeholder='adresse e-mail' value='".$DATABASE_infosparticipant['mail']."'></textarea> <br/><br/>";
					
				if($DATABASE_infosparticipant['gender'] == 1)
				{
					echo "<input type='radio' name='gender' value=1 id='masculin' checked='checked'/><label for='masculin'>Masculin</label>
						<input type='radio' name='gender' value=2 id='feminin'/><label for='feminin'>Féminin</label> <br/><br/>";
				}
				else
				{
					echo "<input type='radio' name='gender' value=1 id='masculin'/><label for='masculin'>Masculin</label>
						<input type='radio' name='gender' value=2 id='feminin' checked='checked'/><label for='feminin'>Féminin</label> <br/><br/>";
				}
				
				$birthMonth = $DATABASE_infosparticipant['birthMonth'];
				
				echo "	<label name='birthyear'>Année de naissance :</label> 
				
						<select name='monthlist' onChange='combo(this, 'birthyear')'>
							<option value=1"; if($birthMonth == 1){echo " selected ";} echo ">janvier</option> 
							<option value=2"; if($birthMonth == 2){echo " selected";} echo ">février</option>
							<option value=3"; if($birthMonth == 3){echo " selected";} echo ">mars</option>
							<option value=4"; if($birthMonth == 4){echo " selected";} echo ">avril</option>
							<option value=5"; if($birthMonth == 5){echo " selected";} echo ">mai</option>
							<option value=6"; if($birthMonth == 6){echo " selected";} echo ">juin</option>
							<option value=7"; if($birthMonth == 7){echo " selected";} echo ">juillet</option>
							<option value=8"; if($birthMonth == 8){echo " selected";} echo ">août</option>
							<option value=9"; if($birthMonth == 9){echo " selected";} echo ">septembre</option>
							<option value=10"; if($birthMonth == 10){echo " selected";} echo ">octobre</option>
							<option value=11"; if($birthMonth == 11){echo " selected";} echo ">novembre</option>
							<option value=12"; if($birthMonth == 12){echo " selected";} echo ">décembre</option>
						</select> 
				
						<select name='yearlist' onChange='combo(this, 'birthyear')'>";		
						
						for($i = $year; $i > $year - 100; $i--)
						{
							echo "<option"; if($i == $DATABASE_infosparticipant['birthYear']){ echo " selected"; } echo ">".$i."</option>";
						}
				
				echo "</select> <br/><br/>
						
						<input type='submit' name='infos' value='Enregistrer' />
					</form>";
				
				echo $MSG_error;
			}
		?>
	</div>
</body>
</html>